    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Outils.LiaisonSqlDB;
import Outils.LiaisonWS;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import server.Monitoring;

/**
 *
 * @author Arnaud
 */
@WebServlet(name = "interrogationIP", urlPatterns = {"/interrogationIP"})
public class interrogationIP extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    LiaisonSqlDB liaisonSqlDB = new LiaisonSqlDB("localhost", "arnueldb", "arnuel", "mysqlarnuel");

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try (PrintWriter out = response.getWriter()) {
            if (request.getSession() == null) {
                redirectToSite(request, response, "dispatcheur");
            } else if (request.getSession().getAttribute("authentified") == null) {
                redirectToSite(request, response, "dispatcheur");
            } else if (!((String) request.getSession().getAttribute("authentified")).equalsIgnoreCase("yes")) {
                redirectToSite(request, response, "dispatcheur");
            } else {

                Date aujourdhui = new Date();
                DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
                        DateFormat.MEDIUM,
                        DateFormat.MEDIUM);
                String date = shortDateFormat.format(aujourdhui);

                try {
                    if (((request.getParameter("ipAddressInt"))) == null) {
                        redirectToSite(request, response, "dispatcheur");

                    } else if (((request.getParameter("ipAddressInt"))).equalsIgnoreCase("")) {
                        request.setAttribute("error", "adresse IP Serveur non renseignée");
                        redirectToSite(request, response, "dispatcheur");
                    }

                    String serveur = (String) (request.getParameter("ipAddressInt"));
                    LiaisonWS liaison = new LiaisonWS(serveur, "localhost");

                    request.setAttribute("ipAddressInt", serveur);

                    if (liaison.getMonitor() == null) {

                        request.setAttribute("error", "Error WS : Laison réseau impossible ");
                        redirectToSite(request, response, "dispatcheur");
                    }

                    if (((request.getParameter("listProcess"))) != null) {

                        int indexTask = liaisonSqlDB.getIndexMax() + 1;

                        request.setAttribute("tasks", liaison.getArrayTask());
                        String cmd = "vide";
                        for (int i = 0; i < liaison.getArrayTask().size(); i++) {
                            if (cmd.contentEquals("vide")) {
                                cmd = liaison.getArrayTask().get(i).getImage() + "," + liaison.getArrayTask().get(i).getPid() + "," + liaison.getArrayTask().get(i).getSession();
                            } else {
                                cmd = cmd + ";" + liaison.getArrayTask().get(i).getImage() + "," + liaison.getArrayTask().get(i).getPid() + "," + liaison.getArrayTask().get(i).getSession();
                            }
                        }

                        liaisonSqlDB.stockerInformation(indexTask, date, serveur, "tasks", cmd);

                    }

                    if (((request.getParameter("memory"))) != null) {
                        int indexMemory = liaisonSqlDB.getIndexMax() + 1;

                        request.setAttribute("memory", liaison.getMemory());
                        liaisonSqlDB.stockerInformation(indexMemory, date, serveur, "memory", Integer.toString(liaison.getMemory()));

                    }
                    if (((request.getParameter("cpu"))) != null) {
                        int indexCpu = liaisonSqlDB.getIndexMax() + 1;

                        request.setAttribute("cpu", liaison.getCpuLoad());
                        liaisonSqlDB.stockerInformation(indexCpu, date, serveur, "cpu", Integer.toString(liaison.getCpuLoad()));

                    }
                    redirectToSite(request, response, "serveurmonitoring.jsp");

                } catch (MalformedURLException ex) {

                    request.setAttribute("error", "Error RMI : URL mal formée");

                    Logger.getLogger(interrogationIP.class.getName()).log(Level.SEVERE, null, ex);
                    redirectToSite(request, response, "dispatcheur");
                } catch (RemoteException ex) {
                    Logger.getLogger(interrogationIP.class.getName()).log(Level.SEVERE, null, ex);
                    request.setAttribute("error", "Error RMI : Laison réseau impossible ");
                    redirectToSite(request, response, "dispatcheur");

                }

            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void redirectToSite(HttpServletRequest request, HttpServletResponse response, String site) throws ExceptionInInitializerError, ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/" + site);
        dispatcher.forward(request, response);
        return;
    }

}
